package com.dw.etl.core.springbatch.processor;

import com.dw.etl.common.constant.CommonConstant;
import com.dw.etl.common.util.SnowflakeIdWorker;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * Created by liujunlei on 2021/6/15.
 */
@Component
public class NullHandleProcessor implements ItemProcessor<Map<String,Object>,Map<String,Object>> {


    /**
     * 空值补全
     * @param stringObjectMap
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> process(Map<String, Object> stringObjectMap) throws Exception {
        Object value;String na_dim_fld;
        Set<String> keySet = stringObjectMap.keySet();
        for(String fld:keySet) {
            value = stringObjectMap.get(fld);
            if(fld.toLowerCase().startsWith(CommonConstant.PRE_DIM) && value==null) {
                stringObjectMap.put(fld,CommonConstant.NULL_DIM_CODE);
                na_dim_fld = fld.toLowerCase().replace("pk_","na_");
                if(keySet.contains(na_dim_fld)) {
                    stringObjectMap.put(na_dim_fld,CommonConstant.NULL_DIM_NAME);
                }
            }
            if(fld.equals("pk")){
                stringObjectMap.put(fld,SnowflakeIdWorker.getId());
            }

        }
        return stringObjectMap;
    }
}
